﻿@{
    ViewBag.Title = "Form"; 
    Layout = "~/Views/Shared/_Form.cshtml";
}
<script>
    layui.use(['jquery', 'form', 'laydate', 'common', 'optimizeSelectOption'], function () {
        var form = layui.form,
            $ = layui.$,
            common = layui.common;
        var itemId = $.request("itemId");
        var parentform = $.request("parentform");
        var currentWindow = !parent[parentform] ? common.currentWindow() : parent[parentform];
        //权限字段
        common.authorizeFields('adminform');

        $(function () {
            form.render();
        });
        wcLoading.close();

        var lock = false;
        //监听提交
        form.on('submit(saveBtn)', function (data) {
            var postData = data.field;
            postData["F_MaterialId"] = itemId;
            if (!lock) {
                lock = true;
                var index = parent.layer.load(0, {
                    shade: [0.5, '#000'], //0.1透明度的背景
                });
                common.ajax({
                    url: '/MaterialManage/Material/GetFormJson',
                    dataType: 'json',
                    data: { keyValue: postData.F_SonMaterialId },
                    async: false,
                    success: function (data) {
                        data["F_SonMaterialId"] = postData.F_SonMaterialId;
                        data["F_Num"] = postData.F_Num;
                        data["F_Description"] = postData.F_Description;
                        var tempdata = currentWindow.layui.table.cache.currentTable;
                        var isHave = false;
                        for (var i = 0; i < tempdata.length; i++) {
                            if (data.F_MaterialCode == tempdata[i].F_MaterialCode) {
                                tempdata[i].F_Num = parseFloat(tempdata[i].F_Num) + parseFloat(postData.F_Num);
                                tempdata[i].Description = tempdata[i].Description + " " + postData.Description;
                                isHave = true;
                                break;
                            }
                        }
                        if (!isHave) {
                            tempdata.push(data);
                        }

                        common.modalMsg("操作成功", "success");
                        currentWindow.layui.table.reload('currentTable', {
                            data: tempdata
                        });
                        parent.layer.close(index);
                        common.modalClose();

                    }
                });
                lock = false;
            }
            return false;
        });
    });
    function search() {
        layui.use(['jquery', 'form', 'common'], function () {
            var $ = layui.$,
                common = layui.common;
            var parentform = window.name;
            common.modalOpen({
                title: "选择物料",
                url: "/MaterialManage/Material/AddForm?parentform=" + parentform + "&name=" + "F_SonMaterialName" + "&value=" + "F_SonMaterialId" + "&ids=" + $('#F_SonMaterialId').val(),
                width: "650px",
                height: "600px",
            });
        });
    }
</script>

<body>
    <div class="layuimini-container">
        <div class="layuimini-main">
            <div class="layui-form layuimini-form" lay-filter="adminform">
                <div class="layui-form-item layui-hide">
                    <label class="layui-form-label required">物料</label>
                    <div class="layui-input-block">
                        <input id="F_SonMaterialName" name="F_SonMaterialName" type="text" lay-verify="required" maxlength="50" autocomplete="off" class="layui-input" onclick="search()" />
                        <input id="F_SonMaterialId" name="F_SonMaterialId" type="text" hidden />
                    </div>
                </div>
                <div class="layui-form-item layui-hide">
                    <label class="layui-form-label required">数量</label>
                    <div class="layui-input-block">
                        <input type="number" pattern="[0-9]*" id="F_Num" name="F_Num" lay-verify="required|number" oninput="if(value.length>8)value=value.slice(0,8)" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item layui-form-text layui-hide">
                    <label class="layui-form-label">备注</label>
                    <div class="layui-input-block">
                        <textarea id="F_Description" name="F_Description" class="layui-textarea" placeholder="请输入备注"></textarea>
                    </div>
                </div>
                <div class="layui-form-item layui-hide">
                    <button class="layui-btn site-demo-active" lay-submit id="submit" lay-filter="saveBtn">确认保存</button>
                </div>
            </div>
        </div>
    </div>
</body>

